package com.hnqcgczy.service.impl;

import com.hnqcgczy.dao.ICategoryDao;
import com.hnqcgczy.dao.IProductDao;
import com.hnqcgczy.dao.imp.CategoryDaoImpl;
import com.hnqcgczy.dao.imp.ProductDaoImpl;
import com.hnqcgczy.db.MyTransaction;
import com.hnqcgczy.service.IProductService;
import com.hnqcgczy.vo.TProduct;
import com.hnqcgczy.vo.TProductCategory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 * 商品服务类
 */
public class ProductServiceImpl implements IProductService {

    @Override
    public List<TProduct> getProductList(int count) {
        //事务
        MyTransaction myTransaction=new MyTransaction(true);
        //ProductDao
        IProductDao proDao=new ProductDaoImpl(myTransaction);
        List<TProduct> list=new ArrayList<TProduct>();
        try {
            list=proDao.getProductList(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //释放连接资源
        myTransaction.closeConn();
        //返回集合
        return list;
    }

    @Override
    public TProduct getProductById(int pid) {
        //事务
        MyTransaction myTransaction=new MyTransaction(true);
        //ProductDao
        IProductDao proDao=new ProductDaoImpl(myTransaction);
       //定义返回对象
        TProduct product=null;
        //调用dao层
        product=proDao.getProductById(pid);
        //释放连接资源
        myTransaction.closeConn();
        //返回对象
        return product;
    }

    @Override
    public List<TProduct> getProductList(String proName, int count) {
        //事务
        MyTransaction myTransaction=new MyTransaction(true);
        //ProductDao
        IProductDao proDao=new ProductDaoImpl(myTransaction);
        //返回结果集合
        List<TProduct> list=new ArrayList<TProduct>();
        //调用dao接口
        try {
            list=proDao.getProductList(proName,count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //释放资源
        myTransaction.closeConn();
        //返回集合
        return list;
    }

    @Override
    public List<TProductCategory> getCategoryList() {
        //事务
        MyTransaction myTransaction=new MyTransaction(true);
        //商品分类dao
        ICategoryDao categoryDao=new CategoryDaoImpl(myTransaction);
        List<TProductCategory> list= new ArrayList<TProductCategory>();
        try {
            list = categoryDao.getCategoryList();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        myTransaction.closeConn();
        return list;
    }
}
